<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-11
 * Time: 下午2:26
 * To change this template use File | Settings | File Templates.
 */
class game extends MY_Controller
{
    public function index()
    {
        $Limit = 20;
        $currentPage = $this->uri->segment(3, 1);//echo $currentPage;exit;
        $offset = ($currentPage - 1) * $Limit;

        $this->load->model('Model_game', 'game');
        $data = $this->game->getApplyList($Limit, $offset);
        $totalNum = $this->game->getApplyCount();

        $this->load->library('pagination');
        $config['base_url'] = '/game/index/';
        $config['total_rows'] = $totalNum;
        $config['per_page'] = $Limit;
        $config['num_links'] = 10;
        $config['uri_segment'] = 3;
        $config['use_page_numbers'] = TRUE;
        $config['anchor_class'] = 'class="page"';
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $this->load->view('game/game', array('navigation' => 'game', 'page_html' => $pageHtml, 'data' => $data));
    }

    /**
     * 用户报名
     */
    public function gameApply()
    {
        $response = array('error' => '0', 'msg' => '用户报名成功', 'code' => 'user_game_apply_success');

        do {
            if (!$this->isLogin()) {
                $response = error(10009);
                break;
            }

            $this->load->model('Model_game', 'game');
            $gameData = $this->game->getGameUser($this->uInfo['passport']);
            if ( !empty($gameData) ) {
                $response = error(10017);
                break;
            }

            $this->load->model('model_weiqi_user', 'wuser');
            $data = $this->wuser->getOneUserInfo($this->uInfo['passport']);
	    $data = $this->wuser->formatData($data);

            if (empty ($data)) {
                $response = error(10015);
                break;
            }

            if ($data['z:row']['GoLevel'] < 6) {
                $response = error(10016);
                break;
            }

            $info = array(
                'passport' => $this->uInfo['passport'],
                'nickname' => $data['z:row']['username'],
                'dan' => $data['z:row']['GoLevel'],
                'chess_will' => $data['z:row']['Clubname'],
            );


            $status = $this->game->joinGame($info);

            if ( !$status ) {
                $response = error(99999);
                break;
            }
        } while (false);

        self::json_output($response, true);
    }
}


//$xml ="1.xml";
